class OnlineEmmitter {
  listeners = [];
  constructor() {
    // 开启事件监听
    window.addEventListener('online', () => {
      this.listeners.forEach((func) => {
        func({ onLine: true });
      });
    });

    window.addEventListener('online', () => {
      console.log('online');
    });
    window.addEventListener('offline', () => {
      console.log('offline');
    });
    window.addEventListener('offline', () => {
      this.listeners.forEach((func) => {
        func({ onLine: false });
      });
    });
  }
  isFunction(func) {
    return typeof func === 'function';
  }

  on(listener) {
    if (this.isFunction(listener)) {
      const index = this.listeners.indexOf(listener);
      if (index === -1) {
        this.listeners.push(listener);
        return () => {
          const index = this.listeners.indexOf(listener);
          this.listeners.splice(index, 1);
        };
      }
    }
  }

  off(listener) {
    if (this.isFunction(listener)) {
      const index = this.listeners.indexOf(listener);
      if (index >= 0) {
        this.listeners.splice(index, 1);
      }
    }
  }
}

class EventBus {
  listeners = [];
  constructor() {
    this._events = {};
  }
  on(fn) {
    if (!fn) {
      return;
    }
    const index = this.listeners.indexOf(fn);
    if (index < 0) {
      events.push(fn);
    }
    return this;
  }
  off(eventName, listener) {
    const listeners = this._events[eventName];
    if (!listeners) {
      return;
    }
    if (!listener) {
      this._events[eventName] = [];
      return;
    }
    const index = listeners.findIndex((item) => item === listener);
    if (index >= 0) {
      listeners.splice(index, 1);
    }
    return this;
  }
}

const obj = new OnlineEmmitter();

obj.on((data) => {
  console.log(data);
});
obj.on((data) => {
  console.log('22', data);
});
